//
//  WFHuaTiViewController.m
//  WangyiFangzuo
//
//  Created by 瑜 on 16/9/12.
//  Copyright © 2016年 111. All rights reserved.
//

#import "WFHuaTiViewController.h"
#import "WFHuaTiTableViewCell.h"
#import <AFNetworking.h>
#import "WFPictureTableViewCell.h"
#import "WFWenzTableViewCell.h"
#import "WFPictureModel.h"
#import "WFWenZiModel.h"
@interface WFHuaTiViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *arrM;
@property(nonatomic,strong)NSMutableArray *arrMM;
@end

@implementation WFHuaTiViewController

- (NSMutableArray *)arrM
{
    if(!_arrM)
    {
        _arrM = [NSMutableArray array];
    }
    return _arrM;
}
- (NSMutableArray *)arrMM
{
    if(!_arrMM)
    {
        _arrMM = [NSMutableArray array];
    }
    return _arrMM;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadData];
    self.view.backgroundColor = [UIColor whiteColor];
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    _tableView.contentInset = UIEdgeInsetsMake(0, 0, 100, 0);
    _tableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.arrMM.count+1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0)
    {
        static NSString *ID = @"collection";
        WFHuaTiTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        if(!cell)
        {
            cell = [WFHuaTiTableViewCell cell];
        }
        return cell;
    }
    else
    {
        WFWenZiModel *model = self.arrMM[indexPath.section-1];
        if([model.type intValue])
        {
            static NSString *ID = @"picture";
            WFPictureTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
            if(!cell)
            {
                cell = [WFPictureTableViewCell cell];
            }
            WFPictureModel *model = self.arrMM[indexPath.section-1];
            cell.model = model;
            NSLog(@"%@",cell.titleLab);
            return cell;
        }
        else
        {
            WFWenZiModel *model = self.arrMM[indexPath.section-1];
            static NSString *ID = @"wen";
            WFWenzTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
            if(!cell)
            {
                cell = [WFWenzTableViewCell cell];
            }
            cell.model = model;
            NSLog(@"%@",cell.titleLab);
            return cell;
        }
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 150;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}
- (void)loadData
{
    AFHTTPSessionManager *manger = [AFHTTPSessionManager manager];
    manger.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", nil];
    [manger GET:@"http://topic.comment.163.com/topic/list/subject/0-10.html" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        for(NSDictionary *dict in responseObject[@"data"][@"subjectList"])
        {
            if([dict[@"type"] intValue] == 1)
            {
                WFPictureModel *model = [WFPictureModel modelWithDict:dict];
                [self.arrMM addObject:model];
            }
            else
            {
                
                WFWenZiModel *model = [WFWenZiModel modelWithDict:dict];
                [self.arrMM addObject:model];
            }
        }
        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
}
@end
